package demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LayoutExample2 {

    private JButton button1;
    private JButton button2;
    private JTextField textField;
    private JLabel southwest;
    private JLabel southeast;
    private JPanel mainPanel;
    private JMenuBar menuBar;

    public LayoutExample2() {
        BuildUI();
        setListener();
        BuildMenus();
    }

    private void BuildMenus() {
        menuBar = new JMenuBar();
        
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        
        JMenu newMenu = new JMenu("New");
        JMenuItem close = new JMenuItem("Close");
        JMenuItem newJavaDoc = new JMenuItem("JavaDoc");
        newMenu.add(newJavaDoc);
        close.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        
        fileMenu.add(newMenu);
        fileMenu.addSeparator();
        fileMenu.add(close);
        
        
        JMenu editMenu = new JMenu("Edit");
        
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
    }

    private void setListener() {
        button1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText("You stuuped");
            }
        });
        
        button2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText("That's deep");
            }
        });
    }

    private void BuildUI() {
        mainPanel = new JPanel();
        BorderLayout layout = new BorderLayout();
        mainPanel.setLayout(layout);
        
        //first and second btns
        JPanel topPanel = new JPanel(new FlowLayout());
        button1 = new JButton("Button one");
        topPanel.add(button1);
        
        button2 = new JButton("Button two");
        button2.setBackground(new Color(153, 51, 255));
        topPanel.add(button2);
        
        JPanel centrelPanel = new JPanel(new BorderLayout());
        
        textField = new JTextField("Very deep thought!!!");
        centrelPanel.add(textField, BorderLayout.CENTER);
        centrelPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        
        JPanel southPanel = new JPanel(new BorderLayout());
        southPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
        southeast = new JLabel("Southeast");
        southeast.setForeground(Color.CYAN);
        southwest = new JLabel("SouthWest");
        southPanel.add(southeast, BorderLayout.EAST);
        southPanel.add(southwest, BorderLayout.WEST);
        
        mainPanel.add(southPanel, BorderLayout.SOUTH);
        mainPanel.add(centrelPanel, BorderLayout.CENTER);
        mainPanel.add(topPanel, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        LayoutExample2 le = new LayoutExample2();
        JPanel main = le.getMainPanel();
        
        JFrame frame = new JFrame();
        frame.setJMenuBar(le.getMenuBar());
        frame.add(main);
        frame.setSize(400, 200);
        frame.setTitle("Composite Layout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public JButton getButton1() {
        return button1;
    }

    public void setButton1(JButton button1) {
        this.button1 = button1;
    }

    public JButton getButton2() {
        return button2;
    }

    public void setButton2(JButton button2) {
        this.button2 = button2;
    }

    public JTextField getButton3() {
        return textField;
    }

    public void setButton3(JTextField tf) {
        this.textField = tf;
    }

    public JLabel getSouthwest() {
        return southwest;
    }

    public void setSouthwest(JLabel southwest) {
        this.southwest = southwest;
    }

    public JLabel getSoutheast() {
        return southeast;
    }

    public void setSoutheast(JLabel southeast) {
        this.southeast = southeast;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public void setMainPanel(JPanel panel) {
        this.mainPanel = panel;
    }

    public JTextField getTextField() {
        return textField;
    }

    public void setTextField(JTextField textField) {
        this.textField = textField;
    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }

    public void setMenuBar(JMenuBar menuBar) {
        this.menuBar = menuBar;
    }

}
